/*
 *所有关于tb_project_workflowstep类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_workflowstepService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.System.IRepositories;
using System.Net;
using System;
using VOL.Core.Utilities.Response;
using VOL.Entity.DomainModels.ApiEntity.Input;
using VOL.Core.EFDbContext;
using VOL.Core.ManageUser;
using VOL.System.IServices;

namespace VOL.System.Services
{
    public partial class tb_project_workflowstepService:Itb_project_workflowstepService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_workflowstepRepository _repository;//访问数据库
        private readonly Itb_project_workflowtableRepository _tableRepository;
        private readonly VOLContext _db;

        [ActivatorUtilitiesConstructor]
        public tb_project_workflowstepService(
            Itb_project_workflowstepRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            Itb_project_workflowtableRepository tableRepository,
            VOLContext db
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _tableRepository = tableRepository;
            _db = db;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent webResponse = new WebResponseContent();
        UserInfo userInfo = UserContext.Current.UserInfo;
        /// <summary>
        /// 项目节点审核添加
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        /// 2023/4/11 wjq
        public WebResponseContent WorkFlowStepAddTs(WorkStepInput_Dto inputDto)
        {
            //获取指定流程项目关系表
            var workFlowTable = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == inputDto.projectId).FirstOrDefault();
            //获取流程表
            Sys_WorkFlow workFlow = _db.Set<Sys_WorkFlow>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            //获取流程项目关系表当前的流程节点
            Sys_WorkFlowStep workFlowStep = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId.ToString() == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            //为流程审核信息赋值
            tb_project_workflowstep tbStep = new tb_project_workflowstep();
            tbStep.workFlowTableId = workFlowTable.id;
            tbStep.workFlowId = workFlow.WorkFlow_Id.ToString();
            tbStep.stepId = workFlowStep.StepId;
            tbStep.stepName = workFlowStep.StepName;
            tbStep.stepType = workFlowStep.StepType;
            tbStep.stepValue = workFlowStep.StepValue;
            tbStep.orderId = workFlowStep.OrderId;
            tbStep.auditId = userInfo.User_Id;
            tbStep.auditor = userInfo.UserTrueName;
            tbStep.auditDate = DateTime.Now;
            tbStep.remark = inputDto.remark;
            //审核结果判断
            if (inputDto.Outcome)
            {
                //流程审核信息的审核状态
                tbStep.auditStatus = 1;
                //关系表的当前流程节点的跳转
                workFlowTable.currentOrderId += 1;
                var step = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId.ToString() == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId);
                //判断流程是否进行结束
                if (step.Count() < 1)
                {
                    //关系表的审核状态修改
                    workFlowTable.auditStatus = 1;
                }

            }
            else
            {
                tbStep.auditStatus = 2;
                if (string.IsNullOrEmpty(inputDto.orderId))
                {
                    workFlowTable.auditStatus = 2;
                }
                else
                {
                    workFlowTable.currentOrderId = inputDto.orderId;
                }
            }

            //基础信息添加
            _repository.Add(tbStep, true);
            //流程关系表修改
            _tableRepository.Update(workFlowTable, true);


            // 返回响应
            return webResponse.OK("success");
        }
        /// <summary>
        /// 新增入账单节点审核添加
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public WebResponseContent newAccountAdd(NewAccountInput_Dto inputDto)
        {
            //获取指定流程项目关系表
            var workFlowTable = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == inputDto.billingId).FirstOrDefault();
            //获取流程表
            Sys_WorkFlow workFlow = _db.Set<Sys_WorkFlow>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            //获取流程项目关系表当前的流程节点
            Sys_WorkFlowStep workFlowStep = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId.ToString() == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            //为流程审核信息赋值
            tb_project_workflowstep tbStep = new tb_project_workflowstep();
            tbStep.workFlowTableId = workFlowTable.id;
            tbStep.workFlowId = workFlow.WorkFlow_Id.ToString();
            tbStep.stepId = workFlowStep.StepId;
            tbStep.stepName = workFlowStep.StepName;
            tbStep.stepType = workFlowStep.StepType;
            tbStep.stepValue = workFlowStep.StepValue;
            tbStep.orderId = workFlowStep.OrderId;
            tbStep.auditId = userInfo.User_Id;
            tbStep.auditor = userInfo.UserTrueName;
            tbStep.auditDate = DateTime.Now;
            tbStep.remark = inputDto.remark;
            //审核结果判断
            if (inputDto.Outcome)
            {
                //流程审核信息的审核状态
                tbStep.auditStatus = 1;
                //关系表的当前流程节点的跳转
                workFlowTable.currentOrderId += 1;
                var step = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId.ToString() == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId);
                //判断流程是否进行结束
                if (step.Count() < 1)
                {
                    //关系表的审核状态修改
                    workFlowTable.auditStatus = 1;
                }

            }
            else
            {
                tbStep.auditStatus = 2;
                if (!string.IsNullOrEmpty(inputDto.orderId))
                {
                    workFlowTable.auditStatus = 2;
                }
                else
                {
                    workFlowTable.currentOrderId = inputDto.orderId;
                }
            }

            //基础信息添加
            _repository.Add(tbStep, true);
            //流程关系表修改
            _tableRepository.Update(workFlowTable, true);
            // 返回响应
            return webResponse.OK("success");
        }
        /// <summary>
        /// 经费分配节点审核添加
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public WebResponseContent newAllotAdd(NewAllotInput_Dto inputDto)
        {
            //获取指定流程项目关系表
            var workFlowTable = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == inputDto.allocationId).FirstOrDefault();
            //获取流程表
            Sys_WorkFlow workFlow = _db.Set<Sys_WorkFlow>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            //获取流程项目关系表当前的流程节点
            Sys_WorkFlowStep workFlowStep = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId.ToString() == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            //为流程审核信息赋值
            tb_project_workflowstep tbStep = new tb_project_workflowstep();
            tbStep.workFlowTableId = workFlowTable.id;
            tbStep.workFlowId = workFlow.WorkFlow_Id.ToString();
            tbStep.stepId = workFlowStep.StepId;
            tbStep.stepName = workFlowStep.StepName;
            tbStep.stepType = workFlowStep.StepType;
            tbStep.stepValue = workFlowStep.StepValue;
            tbStep.orderId = workFlowStep.OrderId;
            tbStep.auditId = userInfo.User_Id;
            tbStep.auditor = userInfo.UserTrueName;
            tbStep.auditDate = DateTime.Now;
            tbStep.remark = inputDto.remark;
            //审核结果判断
            if (inputDto.Outcome)
            {
                //流程审核信息的审核状态
                tbStep.auditStatus = 1;
                //关系表的当前流程节点的跳转
                workFlowTable.currentOrderId += 1;
                var step = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId.ToString() == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId);
                //判断流程是否进行结束
                if (step.Count() < 1)
                {
                    //关系表的审核状态修改
                    workFlowTable.auditStatus = 1;
                }

            }
            else
            {
                tbStep.auditStatus = 2;
                if (!string.IsNullOrEmpty(inputDto.orderId))
                {
                    workFlowTable.auditStatus = 2;
                }
                else
                {
                    workFlowTable.currentOrderId = inputDto.orderId;
                }
            }

            //基础信息添加
            _repository.Add(tbStep, true);
            //流程关系表修改
            _tableRepository.Update(workFlowTable, true);


            // 返回响应
            return webResponse.OK("success");
        }
    }
}
